package com.eling.elcms.cater.service.impl;

import com.eling.elcms.cater.dao.TravelAccountFlowDao;
import com.eling.elcms.cater.model.TravelAccount;
import com.eling.elcms.cater.model.TravelAccountFlow;
import com.eling.elcms.cater.model.view.TravelAccountFlowView;
import com.eling.elcms.cater.service.ITravelAccountFlowService;
import com.eling.elcms.cater.service.ITravelAccountService;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelAccountFlowServiceImpl  extends GenericManagerImpl<TravelAccountFlow,Long> implements ITravelAccountFlowService{

	TravelAccountFlowDao consumerAccountFlowDao;
	
	@Autowired
	private ITravelAccountService travelAccountService;
	
	@Autowired
	public void setConsumerAccountFlowDao(TravelAccountFlowDao consumerAccountFlowDao) {
		this.dao = consumerAccountFlowDao;
		this.consumerAccountFlowDao = consumerAccountFlowDao;
	}

	@Override
	public TravelAccountFlow recordFlow(TravelAccountFlow cond) {
		TravelAccount travelAccount = travelAccountService.get(cond.getTravelAccount().getPkTravelAccount());
		Double balance = travelAccount.getBalance();
		balance = balance - cond.getMoney();
		travelAccount.setBalance(balance);
		cond.setTravelAccount(travelAccount);
		cond.setBalance(balance);
		
		return save(cond);
	}

	@Override
	public TravelAccountFlow saveAttendant(TravelAccountFlow travelAccountFlow) {
		
		TravelAccountFlow taf = new TravelAccountFlow();
		taf.setBalance(travelAccountFlow.getBalance());
		taf.setRechargeType(travelAccountFlow.getRechargeType());
		taf.setOperateDate(travelAccountFlow.getOperateDate());
		taf.setOperator(travelAccountFlow.getOperator());
		taf.setServicePoint(travelAccountFlow.getServicePoint());
		taf.setTravelAccount(travelAccountFlow.getTravelAccount());
		taf.setVersion(0);
		consumerAccountFlowDao.save(taf);
		
//		Long pk = travelAccountFlow.getTravelAccount().getPkTravelAccount();
		TravelAccount ta = travelAccountService.get(travelAccountFlow.getTravelAccount().getPkTravelAccount());
		Double balance = ta.getBalance()+travelAccountFlow.getBalance();
		ta.setBalance(balance);
		travelAccountService.save(ta);
		return taf;
	}

	@Override
	public List<TravelAccountFlowView> employeeCount(Date operateDate, Date operateDateEnd) {
		return consumerAccountFlowDao.employeeCount(operateDate,operateDateEnd);
	}

}
